layui.define(['layer', 'form', 'dropdown', 'util', 'laytpl', 'table', 'glfiles'], function (exports) {
    var MOD_NAME = 'lvybtpl',
        form = layui.form,
        dropdown = layui.dropdown,
        layer = layui.layer,
        laytpl = layui.laytpl,
        table = layui.table,
        glfiles = layui.glfiles,
        util = layui.util;
    lvybtpl = function () {
    };
    lvybtpl.prototype.config = function () {
        return {
            ajax: {
                type: 'get',
                url: false,
                id: 'lvyb',
                offset: 'r',
                width: '750px',
                height: '450px',
                overflow: false,
                detailData: {},
                postUrl: false,
                allclose: false,
                contentTable: 'contentTable',
            },
            ajaxTab: {
                type: 'get',
                url: false,
                id: 'lvyb',
                width: '750px',
                height: '450px',
                detailData: {},
                contentTable: 'contentTable',
            },
            xmSelect: {
                url: false,
                getData: {},
                id: 'lvyb',
                values: [],
                disabled: false,
            },
            form: {
                data: {},
                area: ['700px', '420px'],
                offset: 'auto',
                title: false,
                maxmin: false,
                overflow: true, //默认开启
                tplbefore: function () {
                },//动态渲染之前
                callbefore: function () {
                }, //窗体成功打开后处理回调，在监听表单前
                callback: function (res) {
                },//表单提交后回调
                postbefore: function (d) {
                    //提交前表单处理回调，改变表单上传值
                    return d;
                },
            },
        }
    }

    lvybtpl.prototype.form = function (e) {
        var t = this;
        e = $.extend(t.config().form, e);
        if (e.id == undefined || $('#' + e.id + "_form_html").length < 1) {
            layer.msg('缺少模板');
            return false;
        }
        var content = $('#' + e.id + "_form_html").html();
        $.get(e.url, e.data, function (res) {
            (typeof e.tplbefore === 'function') && e.tplbefore(res);
            laytpl(content).render(res.data, function (reshtml) {
                var layer_index = layer.open({
                    id: e.id + '_form_box',
                    type: 1,
                    anim: 4,
                    area: e.area,
                    title: e.title,
                    maxmin: e.maxmin,
                    offset: e.offset,
                    content: reshtml,
                    btn: ['确定', '取消'],
                    yes: function (index, layero) {
                        $('#' + e.id + '_form_btn_save').click();
                    },
                    success: function (layero, index) {
                        if (e.overflow === true) $('#' + e.id + '_form_box').css('overflow', 'visible');
                        (typeof e.callbefore === 'function') && e.callbefore(layero, index);
                        (typeof e.getdata === 'function') && e.getdata(res);
                        form.render(null, e.id + '_form_lay_filter');//固度表单 id名 + '_form_lay_filter'
                        //固定提交按钮 id + _form_btn_save
                        form.on('submit(' + e.id + '_form_btn_save' + ')', function (data) {
                            if (typeof e.postbefore === 'function') {
                                data.field = e.postbefore(data.field);
                            }
                            if (data.field === false) return false;
                            $.ajax({
                                type: 'POST',
                                encode: e.encode ? e.encode : 0,
                                url: e.url,
                                data: data.field,
                                success: function (res2) {
                                    if (res2.status) {
                                        layer.msg(res2.msg, {time: 1300}, function () {
                                            if (e.tablelayid) table.reload(e.tablelayid);
                                            layer.close(layer_index);
                                        });
                                    } else {
                                        layer.msg(res2.msg);
                                    }
                                    (typeof e.callback === 'function') && e.callback(res2);
                                }
                            });
                            return false;
                        });
                    }
                });
            })
        });

    }
    lvybtpl.prototype.xmSelect = function (e) {
        if (typeof (szoa) !== 'object') {
            layer.msg('未加载独立jquery.js');
            return false;
        }
        if (typeof (xmSelect) !== 'object') {
            layer.msg('未加载szoa.js');
            return false;
        }
        $.post(e.url, e.getData, function (res) {
            var rdata = [];
            $.each(res.data, function (index, item) {
                var temp = {};
                if (item.name != undefined) {
                    temp.name = item.name;
                } else {
                    temp.name = item.real_name;
                }
                temp.value = item.id;
                rdata.push(temp);
            });
            console.log(e.id);
            var _select = xmSelect.render({
                el: '#' + e.id + '_select_box',
                radio: true,
                clickClose: true,
                data: rdata,
                disabled: e.disabled,
                filterable: true,
                on: function (data) {
                    if (data.arr.length == 1) {
                        $('#' + e.id).val(data.arr[0].value);
                        if ($('#' + e.id + '_name') != undefined) {
                            $('#' + e.id + '_name').val(data.arr[0].name);
                        }
                        if (typeof e.changeafter === 'function') {
                            e.changeafter(data.arr)
                        }
                    }
                },
            });
            console.log(e.values);
            if (e.values != undefined) {
                _select.setValue(e.values);
                if (typeof e.setafter === 'function' && e.values.length != 0) {
                    e.setafter(e.values);
                }
            }
        });
    }
    lvybtpl.prototype.ajax = function (e) {
        var t = this;
        e = $.extend(t.config().ajax, e);
        jq = e.jq;
        jq.ajax({
            type: e.type,
            url: e.url,
            data: e.url,
            dataType: 'html',
            success: function (reshtml) {
                var _index = layer.open({
                    id: e.id + '_form_box',
                    type: 1,
                    anim: 4,
                    offset: e.offset,
                    area: [e.width, e.height],
                    title: '详细页面',
                    content: reshtml,
                    btn: ['确定', '取消'],
                    yes: function (index, layero) {
                        $('#' + e.id + '_form_btn_save').click()
                    },
                    success: function (index, layero) {
                        var res2 = $('#' + e.id + 'Content').html();
                        szoa.get(e.postUrl, {}, function (res) {
                            var resdata = $.extend(e.detailData, res.data);
                            laytpl(res2).render(resdata, function (html) {
                                $('#' + e.id + 'ShowContent').append(html);
                                if (typeof $('#form_files_gl_html').val() != 'undefined') {
                                    glfiles.form({
                                        id: 'form',
                                        is_edit: true,
                                        data: resdata.fi
                                    });
                                }
                                if (e.overflow === true) $('#' + e.id + '_form_box').css('overflow', 'visible');
                                (typeof e.callafter === 'function') && e.callafter(layero, index);
                                form.render();
                            });
                        });
                        form.on('submit(' + e.id + '_form_lay_filter)', function (data) {
                            var _data = $('#' + e.id + '_form_lay').serialize();
                            var d2 = {};
                            var d = _data.split('&');
                            $.each(d, function (index, item) {
                                te = [];
                                te = item.split('=')
                                if (te[0] == 'form_files_id') {
                                    d2['files_id'] = te[1];
                                } else {
                                    d2[te[0]] = te[1];
                                }
                            });

                            $.ajax({
                                type: 'POST',
                                url: e.postUrl,
                                data: d2,
                                success: function (res2) {
                                    if (res2.status) {
                                        layer.msg(res2.msg, {time: 1300}, function () {
                                            console.log(e.contentTable);
                                            table.reload(e.contentTable);
                                            if (e.allclose == false) {
                                                layer.close(_index);
                                            } else {
                                                layer.closeAll();
                                            }

                                        });
                                    } else {
                                        layer.msg(res2.msg);
                                    }
                                }
                            });
                            return false;
                        });


                    }
                });
            }
        });

    }

    lvybtpl.prototype.ajaxTab = function (e) {
        var t = this;
        e = $.extend(t.config().ajaxTab, e);
        jq = e.jq;
        jq.ajax({
            type: e.type,
            url: e.url,
            data: {},
            dataType: 'html',
            success: function (reshtml) {
                layer.open({
                    id: e.id + '_content_box',
                    type: 1,
                    anim: 4,
                    offset: 'r',
                    area: [e.width, e.height],
                    title: '详细页面',
                    content: reshtml,
                    btn: ['取消'],
                    success: function (index, layero) {
                        var res2 = $('#' + e.id + 'Content').html();
                        laytpl(res2).render(e.detailData, function (html) {
                            $('#' + e.id + 'ShowContent').append(html);
                            (typeof e.callafter === 'function') && e.callafter(layero, index);
                        });
                    }
                });
            }
        });

    }
    var lvybtpl = new lvybtpl();

    exports(MOD_NAME, lvybtpl)
})